package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"apiserver/handler/sd"
	"apiserver/handler/user"
	"github.com/spf13/viper"
	"github.com/zxmrlc/log"
)


//加载路由
//func Load(g *gin.Engine) *gin.Engine{
//	g.GET("/", func(c *gin.Context){
//		c.String(200, "Hello 三创人2")
//	})

//	return g
//}

func Load(g *gin.Engine) {
	//处理NoRoute
	g.NoRoute(func(c *gin.Context){
		c.String(http.StatusNotFound, "The incorrect API router")
	})

	g.GET("/", home)
	//创建路由分组
	svcd := g.Group("/sd")
	{
		svcd.GET("/health", sd.HealthCheck)
		svcd.GET("/disk", sd.DiskCheck)
		svcd.GET("/cpu", sd.CPUCheck)
		svcd.GET("/RAM", sd.RAMCheck)
	}

	u := g.Group("/v1/users")
	{
		u.POST("", user.Create)
		// /v1/users/1
		u.GET("/:id", func(c *gin.Context){
			log.Infof("id: %s", c.Param("id"))  //"sc"
			log.Infof("URL id: %s", c.Query("id"))  //"1234"
			log.Infof("URL name: %s",c.Query("name")) // "Manu"
			log.Infof("URL value: %s",c.Query("value")) // ""
			log.Infof("URL wtf: %s",c.Query("wtf")) // ""

			log.Infof("URL name: %s", c.DefaultQuery("name", "unknown")) // "Manu"
			log.Infof("URL id: %s", c.DefaultQuery("id", "none")) // "none"
			log.Infof("URL lastname: %s", c.DefaultQuery("lastname", "none")) // ""
		})
	}

}

func home(c *gin.Context){
	log.Info(viper.GetString("url"))
	c.String(200, "hello, Hello")
}

